// pages/addressset/addressset.js
// 请求方法
const HTTP = require("../../utils/http-list.js");
const http = new HTTP();
//getApp()的数据
let app = getApp();
let gd = app.globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: "",
    addList: "",
    // 为了切换第一次记录默认地址索引
    firstInd: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 
   * @param {跳转增加地址页面} event 
   * id为1 为增加 id为其他为修改
   */
  gotoAdd() {
    wx.navigateTo({
      url: "/pages/address/address?id=1"
    })
  },
  /**
   * 
   * @param {*} event 
   */
  /**
   * 
   * @param {切换radio} event 
   */
  onChange(event) {
    let addList = this.data.addList;
    const ind = event.detail - 0;
    const data = {
      cmd: "addAddress",
      uid: gd.userId ,
      id: addList[ind].id,//地址id为空时添加否则修改
      name: addList[ind].name,//姓名
      phone: addList[ind].phone,//手机号
      provinceCityArea: addList[ind].provinceCityArea,//省/市/区
      address: addList[ind].address,//地址
      isDefault: 1,// 0否 1是
      lon: addList[ind].lon,//经度
      lat: addList[ind].lat,//纬度
    }
    http.showLoading("正在设置").postD(data).then((res) => {
      if (res.data.result == 0) {
        let upInd = this.data.firstInd;
        let addInd = "addList[" + (ind - 0) + "].isDefault";
        let up = "addList[" + (upInd - 0) + "].isDefault";

        this.setData({
          radio: event.detail - 0,
          [addInd]: "1",
          [up]: "0",
          firstInd: ind
        })
        wx.hideLoading();
        http.showToast("设置成功");
      }
    })

  },

  /**
   * 
   * @param {编辑地址} event 
   */
  editor(event) {
    const ind = event.currentTarget.dataset.ind;
    wx.navigateTo({
      url: "/pages/address/address?id=2&json=" + JSON.stringify(this.data.addList[ind - 0])
    })
  },
  /**
   * 
   * @param {删除地址} event 
   */
  del(event) {
    const ind = event.currentTarget.dataset.ind;
    if (this.data.addList.length==1){
      wx.showToast({
        title:"还是留一个地址吧",
        icon:"none",
        mask:true
      })
      return;
    }
    http.showLoading("正在删除").postD({
      cmd: "delAddress",
      uid: gd.userId ,
      id: this.data.addList[ind - 0].id
    }).then((res) => {
      if (res.data.result == 0) {
        let list = this.data.addList;
        list.splice(ind - 0, 1);
        this.setData({
          addList: list
        })
        wx.hideLoading()
      }

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    http.postD({
      cmd: "getAddressList",
      uid: gd.userId 
    }).then((res) => {
      if (res.data.result == 0) {
        this.setData({
          addList: res.data.dataList,
          radio: res.data.dataList[0].isDefault == 1 ? 0 : "",
          firstInd: res.data.dataList[0].isDefault == 1 ? 0 : ""
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})